# coding:utf-8

"""
CopyRight(c)  2017-09-15  Fraplus Corp

Author: Xiaodong.yang
Email:  xiaodong.yang@unidt.com
Functions:

we chat server

dependence package:

fralog
handler


该模块为分维的日志模块，应用程序产生的日志首先会保存在本地存盘，同时写入日志服务器

"""

import logging
import logging.config
import datetime
import json
from globalsettings import CUR_MODE
# from influxdb import InfluxDBClient
# from fralog.kafka_logger import KafkaLogger

if CUR_MODE == 'dev':
    from globalsettings_dev import *
else:
    from globalsettings_prod import *
# log = Fralog().getFileLog()
# 第一步，创建一个logger
logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)  # Log等级总开关

# 第二步，创建一个handler，用于写入日志文件
logfile = './fralog.log'
fh = logging.FileHandler(logfile, mode='a')
fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关

# 第三步，再创建一个handler，用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)  # 输出到console的log等级的开关

# 第四步，定义handler的输出格式
fmt = logging.Formatter("%(asctime)s - %(name)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(fmt)
ch.setFormatter(fmt)

# 第五步，将logger添加到handler里面
logger.addHandler(fh)
logger.addHandler(ch)

# _kafka_log = KafkaLogger().set_server(bootstrap_servers='192.168.0.14:9092')
# _kafka_log.start()
# _kafka_log = None

def get_datetimes():
    dates = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    return dates


def getConsoleLog():
    return Fralog().getFileLog()


def getFileLog():
    return Fralog().getFileLog()


class Fralog(object):
    _client_sys = None  # 系统日志
    _client_app = None  # app业务日志
    _app = ''

    def __init__(self):
        super(Fralog, self).__init__()

        """取消InfluxDB"""
        # self._client_app = InfluxDBClient(
        #     host=LOG_SERVER_IP, port=LOG_SERVER_PORT,
        #     username=LOG_SERVER_USER, password=LOG_SERVER_PWD,
        #     database='fra'
        # )
        # self._client_app.create_database(dbname='fra')
        #
        # self._client_sys = InfluxDBClient(
        #     host=LOG_SERVER_IP, port=LOG_SERVER_PORT,
        #     username=LOG_SERVER_USER, password=LOG_SERVER_PWD,
        #     database='syslog'
        # )
        # self._client_sys.create_database(dbname='syslog')

    def set_app(self, app=''):
        """设置app名称"""
        self._app = app
        return self

    def get_app(self, **args):
        """如果用户特别指定app名称，则使用用户指定的名称;
        否则，使用默认名称
        """
        if args.get('product_id') is not None:
            return args.get('product_id')
        else:
            return self._app

    def build_log(self, **args):
        """
        :param product_id: 
        :param args: 
        :return: 
        """
        ret = {
            "measurement": "log",
            "tags": {
            },
            "fields": {
            }
        }
        if args.get('msg') is not None:
            msg = args.get('msg')
            if isinstance(msg, dict):
                msg = json.dumps(msg)
            elif isinstance(msg, str):
                pass
            args['msg'] = msg

        keys = ['level', 'product_id', 'event']
        for key in keys:
            if args.get(key) is not None:
                level = args.get(key)
                ret['tags'][key] = level
                args.pop(key)

        for k in args:
            ret['fields'][k] = args[k]
        return ret

    def debug(self, msg, **args):
        logger.debug(msg)
        app = self.get_app(**args)
        # _kafka_log.debug(msg, app=app)
        #
        # try:
        #     ret = self.build_log(level="debug", event='log', msg=msg, **args)
        #     self._client_sys.write_points([ret])
        # except Exception as e:
        #     logger.error(e)

    def warn(self, msg, **args):
        logger.warning(msg)
        app = self.get_app(**args)
        # _kafka_log.warning(msg, app=app)
        # try:
        #     ret = self.build_log(level="warn", event='log', msg=msg, **args)
        #     self._client_sys.write_points([ret])
        # except Exception as e:
        #     logger.error(e)

    def warning(self, msg, **args):
        logger.warning(msg)
        app = self.get_app(**args)
        # _kafka_log.warning(msg, app=app)
        #
        # try:
        #     ret = self.build_log(level="warn", event='log', msg=msg, **args)
        #     self._client_sys.write_points([ret])
        # except Exception as e:
        #     logger.error(e)

    def error(self, msg, **args):
        logger.error(msg)
        app = self.get_app(**args)
        # _kafka_log.error(msg, app=app)
        #
        # try:
        #     ret = self.build_log(level="error", event='log', msg=msg, **args)
        #     self._client_sys.write_points([ret])
        # except Exception as e:
        #     logger.error(e)

    def info(self, msg, **args):
        logger.info(msg)
        app = self.get_app(**args)
        # _kafka_log.info(msg, app=app) #
        #
        # try:
        #     ret = self.build_log(level="info", event='log', msg=msg, **args)
        #     self._client_sys.write_points([ret])
        # except Exception as e:
        #     logger.error(e)

    def write_log(self, msg='', event='', product_id='', **args):
        pass
        # _kafka_log.write_log(msg, event, product_id, **args)
        # try:
        #     self.info(msg)
        #     ret = self.build_log(event=event, msg=msg, product_id=product_id, **args)
        #     self._client_app.write_points([ret])
        # except Exception as e:
        #     logger.error(e)

    def getFileLog(self):
        return self

    def getConsoleLog(self):
        return self


if __name__ == '__main__':
    log = Fralog().getFileLog().set_app("456")
    log.info('hello')

    # log.debug('This is a test')
    # log.info('Info')
    # log.warning('warn')
    # log.error('error')

    # log.write_log(msg='登录', event='login', product_id='bole', user= 'test01', status = 0)
    # log.write_log(msg='登录', event='login', product_id='bole', user= 'test01', status = 0)

    # from influxdb import InfluxDBClient
    #
    # client = InfluxDBClient(host=LOG_SERVER_IP, port=LOG_SERVER_PORT,
    #                         username=LOG_SERVER_USER, password=LOG_SERVER_PWD,
    #                         database='syslog')
    # print(client.get_list_database())
    # json_body = [
    #     {
    #         "measurement": "students",
    #         "tags": {
    #             "stuid": "54"
    #         },
    #         "time": get_datetimes(),
    #         "fields": {
    #             "score": 94
    #         }
    #     }
    # ]
    # client.drop_database('fra')
    # client.drop_database('bole')
    # client.create_database('fra')
    # client.create_database('syslog')
    # client.write_points(json_body)
    # rows = client.query("select * from log")
    # rets = rows.get_points()
    # for r in rets:
    #     print(r)
